﻿#include "ssVector2.h"
#include "ssAssert.h"

namespace StarSeeker
{
/////////////////////////////////ssFLOAT2X////////////////////////////////
ssFLOAT2X::ssFLOAT2X(void)
{
	x = y = 0.f;
}
ssFLOAT2X::ssFLOAT2X(ssfloat fx, ssfloat fy)
{
	x = fx;
	y = fy;
}
ssFLOAT2X::ssFLOAT2X(const ssFLOAT2X &sf2)
{
	x = sf2.x;
	y = sf2.y;
}
void ssFLOAT2X::operator=(const ssFLOAT2X &sf2)
{
	x = sf2.x;
	y = sf2.y;
}

ssFLOAT2X::operator ssfloat* ()
{
	return m;
}
ssFLOAT2X::operator const ssfloat* () const
{
	return m;
}

void ssFLOAT2X::Set(ssfloat fx, ssfloat fy)
{
	x = fx;
	y = fy;
}
void ssFLOAT2X::Set(const ssfloat *fVal)
{
	x = fVal[0];
	y = fVal[1];
}
void ssFLOAT2X::Zero()
{
	x = y = 0.f;
}
//近似0向量
bool ssFLOAT2X::IsZero(ssfloat eps)
{
	return (Math3D::Abs(x)<eps && Math3D::Abs(y)<eps);
}
//两个向量近似相等
bool ssFLOAT2X::IsApproximate(const ssFLOAT2X& val, ssfloat eps)
{
	return (Math3D::Abs(x - val.x)<eps && Math3D::Abs(y - val.y)<eps);
}
//取反
void ssFLOAT2X::Against()
{
	x = -x;
	y = -y;
}
//求模
ssfloat ssFLOAT2X::Length() const
{
	return Math3D::Sqrt(x*x + y*y);
}
//求模的平方
ssfloat ssFLOAT2X::SquaredLength() const
{
	return x * x + y * y;
}
//单位化
void ssFLOAT2X::Normalize()
{
	ssfloat len = Length();
	SS_ASSERT(len != 0.0f);
	if (len != 0.0f)
	{
		x /= len;
		y /= len;
	}
}
ssFLOAT2X ssFLOAT2X::Normalized() const
{
	ssFLOAT2X ret(*this);
	ret.Normalize();
	return ret;
}
//点乘
ssfloat ssFLOAT2X::Dot(const ssFLOAT2X& val) const
{
	return x*val.x + y*val.y;
}
//叉乘
ssfloat ssFLOAT2X::Cross(const ssFLOAT2X& val) const
{
	return x*val.y - y*val.x;
}
//模近似0
bool ssFLOAT2X::IsZeroLength() const
{
	return (x*x + y*y < Math3D::gc_EPSILON_E6*Math3D::gc_EPSILON_E6);
}

ssFLOAT2X ssFLOAT2X::operator + ( const ssFLOAT2X& val ) const
{
	return ssFLOAT2X(x+val.x, y+val.y);
}
ssFLOAT2X ssFLOAT2X::operator - ( const ssFLOAT2X& val ) const
{
	return ssFLOAT2X(x-val.x, y-val.y);
}
ssFLOAT2X ssFLOAT2X::operator*(const ssFLOAT2X & val) const
{
	return ssFLOAT2X(x * val.x, y * val.y);
}
ssFLOAT2X ssFLOAT2X::operator/(const ssFLOAT2X & val) const
{
	SS_ASSERT(val.x != 0.0f && val.y != 0.0f);
	if (val.x != 0.0f && val.y != 0.0f)
		return ssFLOAT2X(x / val.x, y / val.y);
	else
		return ssFLOAT2X(0, 0);
}
ssFLOAT2X ssFLOAT2X::operator+(ssfloat val) const
{
	return ssFLOAT2X(x+val, y+val);
}
ssFLOAT2X ssFLOAT2X::operator-(ssfloat val) const
{
	return ssFLOAT2X(x - val, y - val);
}
ssFLOAT2X ssFLOAT2X::operator * ( ssfloat val ) const
{
	return ssFLOAT2X(x*val, y*val);
}
ssFLOAT2X ssFLOAT2X::operator / ( ssfloat val ) const
{
	SS_ASSERT(val!=0.0f);
	if (val != 0.0f)
		return ssFLOAT2X(x / val, y / val);
	else
		return ssFLOAT2X(0, 0);
}

void ssFLOAT2X::operator += ( const ssFLOAT2X& val)
{
	x += val.x;
	y += val.y;
}
void ssFLOAT2X::operator -= ( const ssFLOAT2X& val)
{
	x -= val.x;
	y -= val.y;
}
void ssFLOAT2X::operator*=(const ssFLOAT2X & val)
{
	x *= val.x;
	y *= val.y;
}
void ssFLOAT2X::operator/=(const ssFLOAT2X & val)
{
	SS_ASSERT(val.x != 0.0f && val.y != 0.0f);
	if (val.x != 0.0f && val.y != 0.0f)
	{
		x /= val.x;
		y /= val.y;
	}
}
void ssFLOAT2X::operator+=(ssfloat val)
{
	x += val;
	y += val;
}
void ssFLOAT2X::operator-=(ssfloat val)
{
	x -= val;
	y -= val;
}
void ssFLOAT2X::operator *= ( ssfloat val)
{
	x *= val;
	y *= val;
}
void ssFLOAT2X::operator /= ( ssfloat val)
{
	SS_ASSERT(val!=0.0f);
	if (val != 0.0f)
	{
		x /= val;
		y /= val;
	}
}

bool ssFLOAT2X::operator == ( const ssFLOAT2X& val ) const
{
	if (x==val.x && y==val.y)
		return true;
	else
		return false;
}
bool ssFLOAT2X::operator != ( const ssFLOAT2X& val ) const
{
	if (x!=val.x || y!=val.y)
		return true;
	else
		return false;
}
///////////////////////////////////////////////////////////////////////////
}
